package com.wyzz.hr.action;

import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.axis.utils.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.bo.impl.EmpDistinctNo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;

public class ModifyAllUserRole extends BaseAction {

	public ModifyAllUserRole() {
	}

	public String updateUserRoles(String userId, String userRoles,
			HttpSession session) {
		String flt = DWRUtil.checkAuth("userList", "execute");
		if ("error".equalsIgnoreCase(flt))
			return "noauth";
		if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(userRoles))
			return "error";
		if ("demo".equals(session.getAttribute("loginModel"))
				&& "TYHRM".equalsIgnoreCase(EmpDistinctNo.getEmpDistinctNo(
						userId).substring(0, 5)))
			return "pro";
		UserBo userBo;
		Userinfo oldUser;
		userBo = (UserBo) SpringBeanFactory.getBean("userService");
		oldUser = userBo.getUserById(userId);
		if (oldUser == null)
			return "error";
		String newRolesStmp[];
		Integer newRoles[];
		newRolesStmp = null;
		newRoles = null;
		newRolesStmp = userRoles.split(",");
		if (newRolesStmp.length > 45)
			return "roleOver";
		if (newRolesStmp != null) {
			newRoles = new Integer[newRolesStmp.length];
			for (int i = 0; i < newRolesStmp.length; i++)
				newRoles[i] = Integer.valueOf(newRolesStmp[i]);

		}
		if (newRoles != null && !userBo.isRoleNoHasAuth(newRoles, "911")
				&& userBo.checkAuthModule(oldUser, "911")
				&& userBo.getAdminCount("911") < 2)
			return "no";
		Employee currentEmployee = ((Userinfo) session.getAttribute("userinfo"))
				.getEmployee();
		String createrId = currentEmployee.getId();
		String msg = userBo.updateUser(oldUser, newRoles, createrId);
		if (msg.equals("yes"))
			return userRoles;
		try {
			return "error";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	public String doLimitUpdate(String userId, String status, String ip,
			String mac, HttpSession session) {
		String flt = DWRUtil.checkAuth("userList", "execute");
		if ("error".equalsIgnoreCase(flt))
			return "noauth";
		if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(status))
			return "error";
		if ("demo".equals(session.getAttribute("loginModel"))
				&& "TYHRM".equalsIgnoreCase(EmpDistinctNo.getEmpDistinctNo(
						userId).substring(0, 5)))
			return "pro";
		UserBo userBo;
		Userinfo oldUser;
		userBo = (UserBo) SpringBeanFactory.getBean("userService");
		oldUser = userBo.getUserById(userId);
		if (oldUser == null)
			return "error";
		try {
			int statusInt = Integer.parseInt(status.trim());
			oldUser.setUiLevelRestrict(Integer.valueOf(statusInt));
			if (statusInt == 1)
				oldUser.setUiIpRestrict(ip);
			else if (statusInt == 2) {
				oldUser.setUiIpRestrict(ip);
				oldUser.setUiMacRestrict(mac);
			}
			userBo.updateLimit(oldUser);
			return "yes";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	public List getRoleList() {
		return roleList;
	}

	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}

	private static final long serialVersionUID = 0xc53a77b0ae03eebfL;
	private static final String AUTHMODULE = "911";
	private List roleList;
}
